'use strict';

function isObject(obj) {
    return Object.prototype.toString.call(obj) === '[object Object]'
}

function spinnerStart(msg, spinnerString = '|/-\\') {
    const Spinner = require('cli-spinner').Spinner
    const spinner = new Spinner(msg + '%s')
    spinner.setSpinnerString(spinnerString)
    spinner.start()
    return spinner
}

async function sleep(time = 1000) {
    await new Promise((resolve) => setTimeout(resolve, time))
}

function spawn(command, args, options) {
    const win32 = process.platform === 'win32'
    const cmd = win32 ? 'cmd' : command
    const cmdArgs = win32 ? ['/c'].concat(command, args) : args
    return require('child_process').spawn(cmd, cmdArgs, options || {})
}

function spawnAsync(command, args, options) {
    return new Promise((resolve, reject) => {
        const s = spawn(command, args, options)
        s.on('exit', resolve)
        s.on('error', reject)
    })
}

module.exports = {
    isObject,
    spinnerStart,
    sleep,
    spawn,
    spawnAsync
}
